Imports System.IO
Public Class ListViewDemo

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dir As New DirectoryInfo("C:\Windows\")
        'Me.listView1.Columns.Add("No", 30, HorizontalAlignment.Center)
        Me.ListView1.Columns.Add("Name", 120, HorizontalAlignment.Left)
        Me.ListView1.Columns.Add("Size", 50, HorizontalAlignment.Right)
        Me.listView1.Columns.Add("Type", 120, HorizontalAlignment.Right)
        Dim i As Integer = 0
        ListView1.FullRowSelect = True
        ListView1.View = View.Details

        ListView1.CheckBoxes = True


        Dim item As ListViewItem

        Dim f As FileInfo
        For Each f In dir.GetFiles("*.*")
            i = i + 1
            'item = New ListViewItem(i.ToString())
            item = New ListViewItem(f.Name)

            Dim imgIdx As Integer = i Mod 5
            item.ImageIndex = imgIdx

            'item.SubItems.Add(f.Name)
            item.SubItems.Add(f.Length.ToString())
            item.SubItems.Add(f.Attributes.ToString())
            ListView1.Items.Add(item)
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Select Case ComboBox1.Text
            Case "Details"
                ListView1.View = View.Details
            Case "LargeIcon"
                ListView1.View = View.LargeIcon
            Case "List"
                ListView1.View = View.List
            Case "SmallIcon"
                ListView1.View = View.SmallIcon
            Case "Tile"
                ListView1.View = View.Tile
        End Select
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListView1.Items.Clear()
        ListView1.Groups.Clear()

        Dim gArchive As New ListViewGroup("Archive")
        ListView1.Groups.Add(gArchive)
        Dim gSystem As New ListViewGroup("System")
        ListView1.Groups.Add(gSystem)
        Dim gNormal As New ListViewGroup("Normal")
        ListView1.Groups.Add(gNormal)

        Dim dir As New DirectoryInfo("C:\Windows\")
        Dim i As Integer = 0
        Dim item1 As ListViewItem
        Dim attribute As String = ""
        Dim f As FileInfo
        For Each f In dir.GetFiles("*.*")
            i = i + 1

            item1 = New ListViewItem(f.Name)
            item1.SubItems.Add(f.Name)
            item1.SubItems.Add(f.Length.ToString())
            attribute = f.Attributes.ToString()
            item1.SubItems.Add(attribute)
            If attribute.StartsWith("Archive") Then
                gArchive.Items.Add(item1)
            End If
            If attribute.StartsWith("Normal") Then
                gNormal.Items.Add(item1)

            End If
            If attribute.StartsWith("System") Then
                gSystem.Items.Add(item1)
            End If
            ListView1.Items.Add(item1)
        Next

    End Sub

    Private Sub ListView1_ItemActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.ItemActivate
        Dim item As ListViewItem = ListView1.FocusedItem

        MsgBox(item.Text)

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim strChecked As String = ""
        For Each item As ListViewItem In ListView1.CheckedItems
            strChecked += item.SubItems(0).Text + ","
        Next
        If strChecked <> "" Then MsgBox(strChecked)
    End Sub
End Class
